﻿using QuanLyTiemVang.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyTiemVang.Classes;

namespace QuanLyTiemVang.GUI
{
    /// <summary>
    /// Interaction logic for TraCuuKhachHang.xaml
    /// </summary>
    public partial class TraCuuKhachHang : Window, IPhanQuyenHelper
    {
        private QuanLyTiemVang.KHACHHANG kh;

        private List<TextBox> listTextBox;
        private List<RadioButton> listRadioButton;

        public TraCuuKhachHang()
        {
            InitializeComponent();
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            // Begin dragging the window
            this.DragMove();
        }
        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            CurrentUser.Instance.AddForm(this);
            listTextBox = new List<TextBox>();
            listRadioButton = new List<RadioButton>();

            kh = new KHACHHANG();

            listTextBox.Add(txtMaKhachHang);
            listTextBox.Add(txtTenKhachHang);
            listTextBox.Add(txtDiaChi);
            listTextBox.Add(txtTu);
            listTextBox.Add(txtDen);

            listRadioButton.Add(rbtMaKhachHang);
            listRadioButton.Add(rbtTenKhachHang);
            listRadioButton.Add(rbtDiaChi);
            listRadioButton.Add(rbtTienNo);
            listRadioButton.Add(rbtLoaiKhachHang);

            comboLoaiKhachHang.IsEnabled = false;

            // get from database
            comboLoaiKhachHang.ItemsSource = KhachHangDAO.GetAllLoaiKhachHang();
            dgDanhSachKhachHang.ItemsSource = KhachHangDAO.GetAllKhachHang();

            this.rbtMaKhachHang.IsChecked = true;
            this.txtMaKhachHang.IsEnabled = true;
        }

        private void DisableAllControls()
        {
            foreach (var item in listTextBox)
            {
                item.IsEnabled = false;
                item.Clear();
            }
            comboLoaiKhachHang.IsEnabled = false;
            comboLoaiKhachHang.SelectedIndex = -1;
        }

        private void btnTraCuu_Click_1(object sender, RoutedEventArgs e)
        {
            // get information
            if (Decimal.Parse(txtDen.Text.ToString()) < Decimal.Parse(txtTu.Text.ToString()))
            {
                MessageBox.Show("Khoảng tiền nợ sai.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            int i = checkValid();
            if (i != -1)
            {
                switch (i)
                {
                    case 0:
                        kh.MaKhachHang = Int32.Parse(txtMaKhachHang.Text);
                        dgDanhSachKhachHang.ItemsSource = KhachHangDAO.KhachHangLikeId(kh);
                        break;
                        
                    case 1:
                        kh.TenKhachHang = txtTenKhachHang.Text;
                        dgDanhSachKhachHang.ItemsSource = KhachHangDAO.SelectKhachHangLikeTenKhachHang(kh);
                        break;
                    case 2:
                        kh.DiaChi = txtDiaChi.Text;
                        dgDanhSachKhachHang.ItemsSource = KhachHangDAO.KhachHangLikeAddress(kh);
                        break;
                    case 3:
                    case 4:
                        int x, y;
                        bool bMin = Int32.TryParse(txtTu.Text, out x);
                        bool bMax = Int32.TryParse(txtDen.Text, out y);
                        dgDanhSachKhachHang.ItemsSource = KhachHangDAO.KhachHangByDebt(bMin == true ? x : -1, bMax == true ? y : -1);
                        break;
                    case 5:
                        LOAIKHACHHANG lkh = (LOAIKHACHHANG)comboLoaiKhachHang.SelectedItem;
                        kh.MaLoaiKhachHang = lkh.MaLoaiKhachHang;
                        dgDanhSachKhachHang.ItemsSource = KhachHangDAO.SelectKhachHangLikeLoaiKhachHang(kh);
                        break;
                }
                if (dgDanhSachKhachHang.ItemsSource == null)
                    MessageBox.Show("Không tìm thấy dữ liệu.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                dgDanhSachKhachHang.ItemsSource = KhachHangDAO.GetAllKhachHang();
            }
        }

        // mac dinh co 1 radiobutton dc check
        private int checkValid()
        {
            if (comboLoaiKhachHang.IsEnabled)
            {
                if (comboLoaiKhachHang.SelectedIndex == -1)
                {
                    return -1;
                }
                else
                {
                    return 5;
                }
            }
            else
            {
                foreach (var item in listTextBox)
                {
                    if (string.IsNullOrEmpty(item.Text) == false)
                    {
                        return listTextBox.IndexOf(item);
                    }
                }

                return -1;
            }
        }

        private void RadioButton_Checked_Event_Handler(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            int index = listRadioButton.IndexOf(rb);

            if (index != -1)
            {
                this.DisableAllControls();
                switch (index)
                {
                    case 0:
                        txtMaKhachHang.IsEnabled = true;
                        break;
                    case 1:
                        txtTenKhachHang.IsEnabled = true;
                        break;
                    case 2:
                        txtDiaChi.IsEnabled = true;
                        break;
                    case 3:
                        txtTu.IsEnabled = true;
                        txtDen.IsEnabled = true;
                        break;
                    case 4:
                        comboLoaiKhachHang.IsEnabled = true;
                        break;
                    default:
                        break;
                }
            }
        }

        // textbox number only
        private void textBox_NumberOnly_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            CheckIsNumeric(e);
        }

        private void CheckIsNumeric(TextCompositionEventArgs e)
        {
            int result;
            if (!(int.TryParse(e.Text, out result)))
                e.Handled = true;
        }

        private void btnThoat_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        #region implements IPhanQuyenHelper
        void IPhanQuyenHelper.SetAdminFunction()
        {
        }

        void IPhanQuyenHelper.SetStaffFunction()
        {
        }

        void IPhanQuyenHelper.SetCustomerFunction()
        {
            this.Close();
        }

        void IPhanQuyenHelper.SetNonFunction()
        {
            this.Close();
        }
        #endregion

        private void txtTenKhachHang_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Space && e.Key != Key.Back && e.Key != Key.OemQuotes && e.Key != Key.Tab && ((e.Key < Key.A) || (e.Key > Key.Z)))
                e.Handled = true;
        }
    }
}
